import smtplib
# 发送字符串的邮件
from email.mime.text import MIMEText
# 需要 MIMEMultipart 类
from email.mime.multipart import MIMEMultipart

# 设置服务器所需信息
fromEmailAddr = ''  # 邮件发送方邮箱地址
password = ''  # (注意不是邮箱密码，而是为授权码)
toEmailAddrs = ['','']  # 邮件接受方邮箱地址，注意需要[]包裹，这意味着你可以写多个邮件地址群发

def sed(title,content):
    # 设置email信息
    # ---------------------------发送带附件邮件-----------------------------
    # 邮件内容设置
    message = MIMEMultipart()
    # 邮件主题
    message['Subject'] = title
    # 发送方信息
    message['From'] = fromEmailAddr
    # 接受方信息
    message['To'] = ";".join(toEmailAddrs)
    # 邮件正文内容
    message.attach(MIMEText(content, 'plain', 'utf-8'))

    # 构造附件
    # att1 = MIMEText(open('/media/nvidia/jiaxun/20220214.zip', 'rb').read(), 'base64', 'utf-8')
    # att1['Content-type'] = 'application/octet-stream'
    # att1['Content-Disposition'] = 'attachment; filename="20220214.zip"'
    # message.attach(att1)
    # ---------------------------------------------------------------------

    # 登录并发送邮件
    try:
        server = smtplib.SMTP('smtp.163.com')  # qq邮箱服务器地址，端口默认为25
        server.login(fromEmailAddr, password)
        server.sendmail(fromEmailAddr, toEmailAddrs, message.as_string())
        print('success')
        server.quit()
    except smtplib.SMTPException as e:
        print("error:", e)


# if __name__ == '__main__':
#     sed("test","test")